package com.jk.service.account;

import com.jk.dao.account.*;
import com.jk.dto.account.AccUserDTO;
import com.jk.entity.account.*;
import com.jk.util.BeanMapper;
import com.jk.util.PageManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.springframework.dao.DataAccessException;

import java.util.*;

/**
 * ACC_USER service
 * Tue Sep 22 11:02:19 CST 2015 yanghuan
 */
@Service
public class AccUserService {

    @Autowired
    private AccUserDao accUserDao;
    @Autowired
    private AccUserRoleDao accUserRoleDao;
    @Autowired
    private AccRoleDao accRoleDao;
    @Autowired
    private AccRolePermissionDao accRolePermissionDao;
    @Autowired
    private AccPermissionDao accPermissionDao;
    @Autowired
    private AccDepartmentAccountDao accDepartmentAccountDao;

    @Transactional(value = "account", rollbackFor = Exception.class)
    public void save(AccUserDTO accUserDTO) throws DataAccessException {
        AccUser accUser = BeanMapper.map(accUserDTO, AccUser.class);
        String accUserId = accUser.getAccUserId();
        if ("newUser".equals(accUserId)) {
            accUserId = UUID.randomUUID().toString();
            accUser.setAccUserId(accUserId);
            accUser.setAccRegisterTime(new Date());
            accUserDao.insert(accUser);
        } else {
            accUserDao.updateByPrimaryKeySelective(accUser);
            AccDepartmentAccount accDepartmentAccount = new AccDepartmentAccount();
            accDepartmentAccount.setAccUserId(accUserId);
            accDepartmentAccountDao.delete(accDepartmentAccount);
            AccUserRole accUserRole = new AccUserRole();
            accUserRole.setAccUserId(accUserId);
            accUserRoleDao.delete(accUserRole);
        }
        AccDepartmentAccount accDepartmentAccount = new AccDepartmentAccount();
        accDepartmentAccount.setAccDepartmentAccountId(UUID.randomUUID().toString());
        accDepartmentAccount.setAccUserId(accUserId);
        accDepartmentAccount.setAccDepartmentId(accUserDTO.getAccDepartmentId());
        accDepartmentAccountDao.insert(accDepartmentAccount);
        String[] accRoleIds = accUserDTO.getAccRoleIds().split(",");
        for (String accRoleId : accRoleIds) {
            AccUserRole accUserRole = new AccUserRole();
            accUserRole.setAccUserRoleId(UUID.randomUUID().toString());
            accUserRole.setAccUserId(accUserId);
            accUserRole.setAccRoleId(accRoleId);
            accUserRoleDao.insert(accUserRole);
        }
    }

    @Transactional(value = "account", rollbackFor = Exception.class)
    public void delete(String ids) throws DataAccessException {
        String[] strs = ids.split(",");
        for (int i = 0; i < strs.length; i++) {
            String id = strs[i];
            accUserDao.deleteByPrimaryKey(id);
        }
    }

    public AccUser selectByUserId(String accUserId) throws DataAccessException {
        AccUser accUser = accUserDao.selectByPrimaryKey(accUserId);
        if (null != accUser) {
            Set<String> roleIdSet = accUserRoleDao.selectRoleIdSet(accUserId);
            if (null != roleIdSet) {
                List<AccRole> accRoleList = new ArrayList<AccRole>();
                for (String roleId : roleIdSet) {
                    accRoleList.add(accRoleDao.selectByPrimaryKey(roleId));
                }
                accUser.setRoleList(accRoleList);
            }
            return accUser;
        } else {
            return new AccUser();
        }
    }

    public AccUser getByName(String accUserName) throws DataAccessException {
        return accUserDao.getByName(accUserName);
    }

    public List<AccRole> getByUserId(String accUserId) throws DataAccessException {
        Set<String> roleIdSet = accUserRoleDao.selectRoleIdSet(accUserId);
        List<AccRole> accRoles = new ArrayList<AccRole>();
        for (String roleId : roleIdSet) {
            AccRole accRole = accRoleDao.selectByPrimaryKey(roleId);
            if (null != accRole) {
                accRoles.add(accRole);
                Set<String> permissionSet = accRolePermissionDao.getPermissionIdSetByRoleId(roleId);
                List<AccPermission> accPermissions = new ArrayList<AccPermission>();
                if (null != permissionSet) {
                    for (String permissionId : permissionSet) {
                        AccPermission accPermission = accPermissionDao.selectByPrimaryKey(permissionId);
                        if (accPermission != null) {
                            accPermissions.add(accPermission);
                        }
                    }
                }
                accRole.setPermissionList(accPermissions);
            }
        }
        return accRoles;
    }

    public List<AccUser> select(AccUserDTO accUserDTO) throws DataAccessException {
        PageManager pageManager = new PageManager(accUserDTO.getPage(), accUserDTO.getRows());
        accUserDTO.setPage(pageManager.getPage());
        accUserDTO.setRows(pageManager.getPageSize());
        List<AccUser> accUserList = accUserDao.select(accUserDTO);
        for (AccUser accUser : accUserList) {
            String accUserId = accUser.getAccUserId();
            Set<String> roleIdSet = accUserRoleDao.selectRoleIdSet(accUserId);
            List<AccRole> accRoleList = new ArrayList<AccRole>();
            for (String roleId : roleIdSet) {
                accRoleList.add(accRoleDao.selectByPrimaryKey(roleId));
            }
            accUser.setRoleList(accRoleList);
        }
        return accUserList;
    }

    public int total(AccUserDTO accUserDTO) throws DataAccessException {
        return accUserDao.total(accUserDTO);
    }

    public int updateByPrimaryKey(AccUser accUser) throws DataAccessException {
        return accUserDao.updateByPrimaryKey(accUser);
    }

}

